﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Base.Designer;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.Designer.Org
{
	public class RoleSymbol : Symbol
	{
		public RoleSymbol()
			: base()
		{
			init();
		}

		private void init()
		{
			this.BeginUpdate();

			this.Name = "Role";
			this.Border = BorderType.RoundedRectangle;
			this.BackColor = Color.Plum;
			this.BackColor2 = Color.White;
			this.BackBrushMode = BackgroundBrushMode.LinearGradient;
			this.MinimumSize = new Size(150, 40);
			this.NameTextBox.AutoSize = false;
			this.AllowDrop = true;

			//context Menu
			this.ContextMenuStrip.Items.Add("Add User", Resources.png_users, new EventHandler(onAddUser_Click));

			this.EndUpdate();
		}

		public override void InitializeNew(IServiceProvider serviceProvider)
		{
			base.InitializeNew(serviceProvider);
		}

		public override void InitializeLoaded()
		{
			base.InitializeLoaded();
		}

		#region Public Members

		#region Override

		public override bool CanDrag
		{
			get { return true; }
		}

		public override bool IsDropAllowed(object obj, Point location, MouseButtons button)
		{
			return
				( button == MouseButtons.Right &&
				obj is OrgSymbol ||
				obj is OrgUnitSymbol ||
				obj is RoleSymbol ||
				obj is UserSymbol );
		}

		public override bool CheckMouseOver(Point location)
		{
			return base.CheckMouseOver(location);
		}

		public override void DropObject(object obj, Point location, MouseButtons button)
		{
			if ( obj != this ) {
				this.DesignerViewControl.AddRoute((DesignerControl)obj, this);
			} else {
				base.DropObject(obj, location, button);
			}
		}

		public override object GetDragObject(Point location, MouseButtons button)
		{
			return this;
		}

		public override void PaintBackground(PaintEventArgs pe)
		{
			base.PaintBackground(pe);

			Rectangle rc = this.ClientRectangle;
			Point pt = this.PointToDesignerView(new Point(rc.X, rc.Bottom - 16));
			pe.Graphics.DrawImage(Resources.png_users, pt.X, pt.Y);
		}

		#endregion

		#endregion

		protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
		{
			base.OnMouseClick(e);
		}

		private void onAddUser_Click(object sender, EventArgs e)
		{
			int index = this.DesignerViewControl.AddDesignerControl(new UserSymbol());
			this.DesignerViewControl.DesignerControls[index].Location =
				new Point(Location.X, Location.Y + Height + 40);
			this.DesignerViewControl.AddRoute(this, DesignerViewControl.DesignerControls[index]);
		}

	}
}
